﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Assert;
using CommunitySystem.Ctrls;
using CommunitySystem.PublicOfficialSelectionService;
using CommunitySystem.Views;

namespace CommunitySystem
{
    public partial class ChengJiControl : AsyncCtrl, IReturnListenerc
    {
        #region IReturnListenerc 成员
        public void BeginReturn()
        {
            Load();
        }
        #endregion
        public NavMoveSwitch Nav { get; set; }
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        private PublicOfficialSelectionService.PublicOfficialSelectionServiceClient Client1 =
            new PublicOfficialSelectionService.PublicOfficialSelectionServiceClient();
        private ObservableCollection<PublicOfficialSelectionService.PublicOfficialRecordModel> selectedempmodel =
            new ObservableCollection<PublicOfficialSelectionService.PublicOfficialRecordModel>();
        public ObservableCollection<PublicOfficialSelectionService.PublicOfficialRecordModel> empmodel =
            new ObservableCollection<PublicOfficialSelectionService.PublicOfficialRecordModel>();
        public ChengJiControl()
        {
            InitializeComponent();
            for (int i = DateTime.Today.Year; i > DateTime.Today.Year - 10; i--)
            {
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Content = i.ToString();
                cbi.Tag = i;
                nian.Items.Add(cbi);
            }
            nian.SelectedIndex = 0;
            Load();
        }

        public void Load()
        {
            selectedempmodel.Clear();
            //Client1.GetPublicOfficialRecordbyEmpIDCompleted += new EventHandler<PublicOfficialSelectionService.GetPublicOfficialRecordbyEmpIDCompletedEventArgs>(Client1_GetPublicOfficialRecordbyEmpIDCompleted);
            //Client1.GetPublicOfficialRecordbyEmpIDAsync(0, true,DateTime.Today.Year - nian.SelectedIndex);
        }

        void Client1_GetPublicOfficialRecordbyEmpIDCompleted(object sender, PublicOfficialSelectionService.GetPublicOfficialRecordbyEmpIDCompletedEventArgs e)
        {
            DGRole.ItemsSource = e.Result;
        }

        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as PublicOfficialRecordModel;
            if (seljw != null)
            {
                Client1.BiShiCompleted -= new EventHandler<PublicOfficialSelectionService.BiShiCompletedEventArgs>(Client1_BiShiCompleted);
                Client1.BiShiCompleted += new EventHandler<PublicOfficialSelectionService.BiShiCompletedEventArgs>(Client1_BiShiCompleted);
                Client1.BiShiAsync(seljw.PublicOfficialRecordID);
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要生成笔试通知的人员！");

        }

        void Client1_BiShiCompleted(object sender, PublicOfficialSelectionService.BiShiCompletedEventArgs e)
        {
            if(e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("生成成功!");
                Load();
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("该人员已生成过笔试通知！");
            }
        }

        private void EditData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as PublicOfficialRecordModel;
            if (seljw != null)
            {
                Client1.MianShiCompleted -= new EventHandler<MianShiCompletedEventArgs>(Client1_MianShiCompleted);
                Client1.MianShiCompleted += new EventHandler<MianShiCompletedEventArgs>(Client1_MianShiCompleted);
                //Client1.MianShiAsync(seljw.PublicOfficialRecordID);
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要生成面试通知的人员！");

        }

        void Client1_MianShiCompleted(object sender, MianShiCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("生成成功!");
            Load();
        }

        

        private void ChaData_Click(object sender, RoutedEventArgs e)
        {
            Load();
        }

        private void BiShiData_Click(object sender, RoutedEventArgs e)
        {
            ChengJi_BiShiControl view = new ChengJi_BiShiControl(DateTime.Today.Year - nian.SelectedIndex);
            //view.Nav = Nav;
            Nav.ShowNextPage("笔试人员-详细信息", view, this);
        }
        private void MianShiData_Click(object sender, RoutedEventArgs e)
        {
            //ChengJi_MianShiControl view = new ChengJi_MianShiControl(DateTime.Today.Year - nian.SelectedIndex);
            //view.Nav = Nav;
            //Nav.ShowNextPage("面试人员-详细信息", view, this);
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            foreach (PublicOfficialSelectionService.PublicOfficialRecordModel p in DGRole.ItemsSource)
            {
                p.IsSelected = (bool)cb.IsChecked;
                //CheckBox cb1 = DGRole.Columns[0].GetCellContent(p) as CheckBox;
                //cb1.IsChecked = p.IsSelected;
                if (p.IsSelected)
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(p);
                    }
                }
                else
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() != null)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault());
                    }
                }
            }

        }
    }
}
